package com.itcast.googleplay.adapter;

import java.util.List;

import com.itcast.googleplay.R;
import com.itcast.googleplay.http.CZApi;
import com.itcast.googleplay.utils.UIUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


//BaseAdapter	ListView
//PagerAdapter	ViewPager
//FragmentPagerAdapter	ViewPager添加的是FragmentoncreateView方法返回view

public class MyPagerAdapter extends PagerAdapter {
	private List<String> imgUrlList;
	private DisplayImageOptions options;
	public MyPagerAdapter(List<String> imgUrlList) {
		this.imgUrlList = imgUrlList;
		
		//初始图片缓存方式代码
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_default)//在加载图片的时候,展示的默认图片
				
				.showImageForEmptyUri(R.drawable.ic_default)//uri传递出现问题时展示的图片
				
				.showImageOnFail(R.drawable.ic_default)//加载网络失败的时候,展示的图片
				
				.cacheInMemory(true)//是否需要内存缓存
				
				.cacheOnDisk(true)//是否需要磁盘缓存
				
				.bitmapConfig(Bitmap.Config.RGB_565)//图片的渲染方式,来16位图
				
				.considerExifParams(true)//是否支持图片自动调整,让其正常图像显示
		
				.build();//设置圆角半径
	}
	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}
	
	//(0,1,2,3......100,20多个亿)
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		ImageView imageView = new ImageView(UIUtils.getContext());
		//ImageLoader  xutils BitmapUtils
		//参数一:图片链接地址
		//参数二:显示图片的控件
		//参数三:图片缓存配置(内存,磁盘,圆角)
		//http://127.0.0.1:8090/image?name=app/com.youyuan.yyhl/icon.jpg;
		ImageLoader.getInstance().displayImage(CZApi.BASEIMGURL+imgUrlList.get(position%imgUrlList.size()), imageView, options);
		container.addView(imageView);
		
		return imageView;
	}
	
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View)object);
	}
}
